package convert

import (
	"strconv"

	"github.com/shopspring/decimal"
)

func ConvertToString(in interface{}) string {
	switch t := in.(type) {
	case bool:
		if t {
			return "1"
		} else {
			return "0"
		}
	case int:
		return strconv.FormatInt(int64(t), 10)
	case int32:
		return strconv.FormatInt(int64(t), 10)
	case int64:
		return strconv.FormatInt(t, 10)
	case float32:
		dc := decimal.NewFromFloat32(t)
		return strconv.FormatFloat(dc.InexactFloat64(), 'f', -1, 32)
	case float64:
		return strconv.FormatFloat(t, 'f', -1, 64)
	case string:
		return t
	default:
		return ""
	}
}

func StringToBool(in string) bool {
	return in == "1"
}

func StringToInt(in string) int64 {
	out, _ := strconv.ParseInt(in, 10, 64)
	return out
}

func StringToFloat32(in string) float32 {
	return float32(StringToFloat64(in))
}

func StringToFloat64(in string) float64 {
	dc, err := decimal.NewFromString(in)
	if err != nil {
		return 0
	} else {
		return dc.InexactFloat64()
	}
}
